// import { PRIVATE_KEY } from '../consts'
// import jwt from 'jsonwebtoken'

const { Admins } = require('../models/admins')
const jwt = require('jsonwebtoken')
const { PRIVATE_KEY } = require('../consts')


//管理员登录
async function loginAdmin(req, res, next) {
    try {
        const { username, password } = req.body;
        const user = await Admins.find({ username, password })
        if (user.length) {
            const role = user[0].role === "1" ? "超级管理员" : "普通管理员"
            const _id = user[0]._id;//
            let token = jwt.sign({ username, role, _id, exp: Date.now() / 1000 + (60 * 60 * 24) }, PRIVATE_KEY);
            res.send({
                code: 1,
                data: {
                    userInfo: { username, role },
                    token
                }
            })
        } else {
            res.send({
                code: 0,
                msg: '用户不存在'
            })
        }

    } catch (error) {
        console.log("err", error);
        next(error)
    }
}
//获取管理员列表
 async function getAdminsList(req, res, next) {
    try {
        // console.log(req.body);
        const {pageNumber,pageSize}=req.body
        const totalshuju = await Admins.find()
        const total = totalshuju.length
        const data = await Admins
        .find()
        .limit(pageSize - 0) // 设置请求的条数
        .skip((pageNumber - 1) * pageSize) // 设置跳过的条数
        // console.log(data);
        res.send({ code: 1, data:{data, ...req.body,total} });
    } catch (error) {
        next(error);
    }
}
//删除管理员
 async function deleteOneAdmin(req, res, next) {
    try {
        const { id } = req.query;
        const data = await Admins.findByIdAndDelete(id);
        res.send({ code: 1, data, })
    } catch (error) {
        next(error);
    }
}
//新增管理员
 async function addOneAdmin(req, res, next) {
    try {
        let { username, password, role } = req.body;
        if (!(username && password && role)) {
            res.send({ code: 0, msg: "参数错误!" })
            return;
        }
        //查看是否重名
        const has = await Admins.find({ username });
        if (has.length) {
            res.send({ code: 0, msg: "用户名重复！" });
            return;
        }
        const ret = await Admins.create({ username, password, role });
        console.log('ret', ret)
        if (ret) {
            res.send({ code: 1, msg: '添加成功' });
        } else {
            res.send({ code: 0, msg: '添加失败' })
        }
    } catch (error) {
        next(error);
    }
}
//获得一个管理员的信息
async function getOneAdmin(req,res,next){
    try {
        // console.log(req.body);
        const {_id} = req.body
        const data = await Admins.find({_id});
        res.send({ code: 1, data, })
    } catch (error) {
        
    }
}
//修改一个管理员的信息
async function updateOneAdmin(req,res,next){
    try {
        const {_id,values} = req.body
        const data = await Admins.update({_id},values)
        res.send({
            code:1,
        })
    } catch (error) {
        console.log('error',error);
    }
}
module.exports = {
    loginAdmin,getAdminsList,deleteOneAdmin,addOneAdmin,getOneAdmin,updateOneAdmin

};